package com.xh3140.android.widget.button;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.LinearLayoutCompat;


/**
 * 按钮组控件
 */
public class ButtonGroup extends LinearLayoutCompat {
    /**
     * 按钮位置
     */
    private static final int POSITION_NO = -1;
    private static final int POSITION_START = 0;
    private static final int POSITION_BETWEEN = 1;
    private static final int POSITION_END = 2;

    public ButtonGroup(@NonNull Context context) {
        super(context);
    }

    public ButtonGroup(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public ButtonGroup(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    /**
     * 设置子按钮位置
     *
     * @param button   子按钮
     * @param position 位置标志
     */
    private void setChildPosition(Button button, int position) {
        final boolean isHorizontal = getOrientation() == HORIZONTAL;
        if (position == POSITION_START) {
            button.setGroupLocation(isHorizontal ? Button.GroupLocation.LEFT : Button.GroupLocation.TOP);
        } else if (position == POSITION_BETWEEN) {
            button.setGroupLocation(isHorizontal ? Button.GroupLocation.HORIZONTAL_BETWEEN : Button.GroupLocation.VERTICAL_BETWEEN);
        } else if (position == POSITION_END) {
            button.setGroupLocation(isHorizontal ? Button.GroupLocation.RIGHT : Button.GroupLocation.BOTTOM);
        } else {
            button.setGroupLocation(null);
        }
    }

    /**
     * 添加子按钮之前配置按钮状态
     * 主要设置按钮的位置信息和宽度权重
     */
    private void addViewCheckConfig(View child, int index, ViewGroup.LayoutParams params) {
        if (!(child instanceof Button)) {
            throw new IllegalArgumentException("Cannot add a not Button view to a ButtonGroup.");
        }
        final int childCount = getChildCount();
        final Button button = (Button) child;
        if (childCount == 0) {
            setChildPosition(button, (index == 0) ? POSITION_NO : POSITION_START);
        } else if (childCount == 1) {
            final Button first = (Button) getChildAt(0);
            if (index == 0) {
                setChildPosition(button, POSITION_START);
                setChildPosition(first, POSITION_END);
            } else {
                setChildPosition(first, POSITION_START);
                setChildPosition(button, POSITION_END);
            }
        } else {
            if (index == 0) {
                final Button first = (Button) getChildAt(0);
                setChildPosition(button, POSITION_START);
                setChildPosition(first, POSITION_BETWEEN);
            } else if (index == -1 || index == childCount) {
                final Button last = (Button) getChildAt(childCount - 1);
                setChildPosition(last, POSITION_BETWEEN);
                setChildPosition(button, POSITION_END);
            } else {
                setChildPosition(button, POSITION_BETWEEN);
            }
        }
        if (params instanceof LayoutParams && ((LayoutParams) params).weight == 0f) {
            ((LayoutParams) params).weight = 1f;
        }
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        addViewCheckConfig(child, index, params);
        super.addView(child, index, params);
    }

    @Override
    protected boolean addViewInLayout(View child, int index, ViewGroup.LayoutParams params, boolean preventRequestLayout) {
        addViewCheckConfig(child, index, params);
        return super.addViewInLayout(child, index, params, preventRequestLayout);
    }
}
